﻿using Cofe.Core;
using Cofe.Core.Implements;
using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using Cofe.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if NETFX_CORE
using Windows.Storage;
using Windows.Storage.Pickers;
#endif

namespace Cofe.IO.Implements
{
#if NETFX_CORE
    public class WindowsStorageVolumeFactory : IVolumeFactory
    {

        /// <summary>
        /// Construct a WindowsStorage using a StorageFolder, please make sure your folder is previously registered in 
        /// Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList or MostRecentlyUsedList,
        /// or UnauthorizedException will be thrown.
        /// </summary>
        /// <param name="volumeRegistrar"></param>
        /// <param name="volumeId"></param>
        /// <param name="paramString"></param>
        /// <returns></returns>
        public async Task<IVolume> ConstructAsync(IVolumeRegistrar volumeRegistrar, string volumeId, string paramString)
        {
            var dic = ParamParser.StringToDictionary(paramString);
            if (!dic.ContainsKey("VolumePath"))
                throw new Exception("VolumePath not exists.");
            string volPath = dic["VolumePath"];
            string eventSource = dic.ContainsKey(ParamStringNames.EventSource) ?
                dic[ParamStringNames.EventSource].ToLower() : "manual"; //Manual/Auto = use ManualCofeSystemEventSource.                

            StorageFolder rootDir = await StorageFolder.GetFolderFromPathAsync(volPath);
            var volumeInfo = new VolumeInfo() { FactoryId = FactoryId, ParamString = paramString, VolumeId = volumeId };

            switch (eventSource)
            {
                case "none":
                    return new Volume(FactoryId, volumeId, paramString, dic,
                       new WindowsStorageDirectoryLister(rootDir));
                default:
                    var manualSource = new ManualCofeSystemEventSource(volumeInfo);
                    return new Volume(volumeInfo, ParamParser,
                        new WindowsStorageDirectoryLister(rootDir) { ManualEventSource = manualSource }, manualSource);
            }
        }


        #region Data

        private IEventHub _eventHub = null;
        private IParamParser _paramParser = new ParamParser();

        #endregion


    #region Public Properties

        public IEventHub EventHub
        {
            get { return _eventHub ?? CofeServices.ServiceLocater.FindService<IEventHub>(); }
            set { _eventHub = value; }
        }

        public IParamParser ParamParser
        {
            get { return _paramParser ?? CofeServices.ServiceLocater.FindService<IParamParser>(); }
            set { _paramParser = value; }
        }


        public string FactoryId
        {
            get { return "CofeIO"; }
        }


        #endregion
    }
#endif
}
